<?php namespace Admin\Controller;

use Common\Model\Brand;

class BrandController extends CommonController{
	private $db;
		
	
	public function __init(){
		
		$this->db=new Brand;
		
		parent::__init();
		
	}
	
	public function index(){
		
		$data=$this->db->orderBy('bid','ASC')->get();
		
		View::with('data',$data);
		
		View::make();
	}
	
	public function add(){
		if(IS_POST){
//			var_dump($_POST);die;
			$result=$this->db->store();
			
			if(!$result){
				View::error($this->db->getError());
			}
			View::success('添加成功',U('index'));
		}
						
		View::make();
	}
	
	
	public function edit(){
		if(IS_POST){

			$result=$this->db->edit();
			
			if(!$result){
				View::error($this->db->getError());
			}
			View::success('编辑成功',U('index'));
		}
						
		$bid=Q('get.bid',0,'intval');
		
		$oldData=$this->db->where('bid',$bid)->find();
		
//		var_dump($oldData);die;
		View::with('oldData',$oldData);
		View::make();
	}
	
	
	public function upload()
{
	if(isset($_POST['logo'])){
		return $_POST['logo'];
	}
    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
    if (empty($file)) {
        $this->ajax(Upload::getError());
    } else {
        /** $file内部就是以下这个数组
            $file = array(
                0 => array(
                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
                'image' => 1
            ),
        );**/
        //上传成功，把上传好的信息返给js
        $data = $file[0];
        echo json_encode($data);exit;
    }
}
	
	
	public function del(){
		$bid=Q('get.bid',0,'intval');
//		获取bid
		$this->db->where('bid',$bid)->delete();
//		删除对应的品牌

		$goodsModel= new \Common\Model\Goods;		
		$gid=$goodsModel->where('brand_bid',$bid)->pluck('gid');
//		根据bid来获取品牌下的商品id
		$goodsModel->where('brand_bid',$bid)->delete();
//		删除品牌下的商品
		
		$g_dModel= new \Common\Model\Goods_detail;		
		$g_dModel->where('goods_gid',$gid)->delete();
//		根据获取的商品id删除商品信息
		
		$g_aModel = new \Common\Model\Goods_attr;		
		$g_aModel->where('goods_gid',$gid)->delete();
//		根据获取的商品id删除商品属性
				
		View::success('删除成功',U('index'));
	}
	
	
	
	
}
	
	
	
	
	
	
	
	
	
	?>